﻿using System;
using System.Data;

namespace FDStudio.Core.Extensions
{
    /*
     CreatedOn: 2018-02-01
     CreatedBy: Jackie Lee(天宇遊龍)
     */
    public static class DataReaderExtension
    {
        /// <summary>
        /// 读取基本数据类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public static T Read<T>(this IDataReader reader, string name) where T : struct
        {
            var res = reader[name];
            if (res == null || res == DBNull.Value)
            {
                return default(T);
            }
            return (T)Convert.ChangeType(res, typeof(T));
        }

        /// <summary>
        /// 读取字符串
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public static string ReadString(this IDataReader reader, string name)
        {
            var res = reader[name];
            if (res == null || res == DBNull.Value)
            {
                return string.Empty;
            }
            return res.ToString();
        }
    }
}
